package com.chenxin.wms.process.check.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.WmsTaskStatus;
import com.chenxin.common.enums.WmsTaskSubType;
import com.chenxin.common.enums.WmsTaskType;
import com.chenxin.common.enums.wms.TaskStatus;
import com.chenxin.common.enums.wms.TaskType;
import com.chenxin.common.enums.wms.ZoneType;
import com.chenxin.common.exception.wms.WmsException;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.wms.domain.TaskEntity;
import com.chenxin.wms.domain.WcsTaskEntity;
import com.chenxin.wms.domain.dto.StockCheckExecuteParams;
import com.chenxin.wms.domain.po.LocationInfo;
import com.chenxin.wms.mapper.LocationMapper;
import com.chenxin.wms.mapper.TaskMapper;
import com.chenxin.wms.mapper.WcsTaskMapper;
import com.chenxin.wms.process.check.IProcStockCheckExecute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProcStockCheckExecuteImpl implements IProcStockCheckExecute {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private WcsTaskMapper wcsTaskMapper;
    @Autowired
    private LocationMapper locationMapper;

    @Override
    public ProcResult call(StockCheckExecuteParams params) {
        if (params == null) {
            return ProcResult.error("check_execute_error001", "参数不能为空");
        }
        String contNo = params.getContNo();
        String comments = params.getComments();
        Long creator = params.getCreator();
        Date workDate = new Date();
        if (StringUtils.isEmpty(contNo)) {
            return ProcResult.error("check_execute_error002", "托盘不能为空");
        }
        // 根据托盘条码查询盘点任务
        QueryWrapper<TaskEntity> checkQW = new QueryWrapper<>();
        checkQW.lambda().eq(TaskEntity::getFromContNo, contNo)
                .eq(TaskEntity::getTaskType, WmsTaskType.CHECK.getCode())
                .eq(TaskEntity::getTaskSubType, WmsTaskSubType.CHECK_OUT.getCode())
                .eq(TaskEntity::getTaskStatus, WmsTaskStatus.INITIAL.getCode());

        List<TaskEntity> checkTasks = taskMapper.selectList(checkQW);
        if (checkTasks == null || checkTasks.size() == 0) {
            return ProcResult.error("check_execute_error003", "未找到托盘【" + contNo + "】且状态【初始】的盘点任务！");
        }
        // 托盘可能存在多条盘点任务
        for (TaskEntity checkTask : checkTasks) {
            // 更新盘点任务（状态，执行信息等）
            checkTask.setTaskStatus(WmsTaskStatus.PROGRESS.getCode());
            checkTask.setComments(comments);
            checkTask.setUpdater(creator);
            checkTask.setUpdateDate(workDate);
            taskMapper.updateById(checkTask);
            // 根据任务的始发位置获取其库存类型
            LocationInfo fromLocation = locationMapper.selectLocationByNo(checkTask.getFromPosition());
            if (fromLocation == null) {
                throw new WmsException("check_execute_error004", "托盘【" + contNo + "】的始发位置【" + checkTask.getFromPosition() + "】未在货位表中配置！");
            }
            String zoneType = fromLocation.getZoneType();
            // 自动库盘点出库需下发设备任务
            if (ZoneType.AUTO.getCode().equals(zoneType)) {
                WcsTaskEntity wcsTask = new WcsTaskEntity();
                wcsTask.setRequestPk(String.valueOf(System.currentTimeMillis()));
                wcsTask.setDvcNo(fromLocation.getDvcNo());
                wcsTask.setContNo(contNo);
                wcsTask.setTrkType(TaskType.WCS_IN.getCode());
                wcsTask.setFrmPos(checkTask.getFromPosition());
                wcsTask.setToPos(checkTask.getToPosition());
                wcsTask.setCurPos(checkTask.getFromPosition());
                wcsTask.setTrkStatus(TaskStatus.INIT.getCode());
                wcsTask.setCreator(creator);
                wcsTask.setCreateDate(workDate);
                //Long trkId = wcsTaskProcess.procWmsToWcsInsert(wcsTask);
            }
        }
        return ProcResult.success("托盘【" + contNo + "】盘点任务执行成功！");
    }
}
